const express=require('express');
let router=express.Router();
let myQuery=require('../tools/db');
let {apiUrl}=require('../tools/config')
//搜索接口
router.get('/search', async (req, res, next) => {
    let { keyword, pageNum = 6, page = 1 } = req.query;
    let startNum = (page - 1) * pageNum;
    let tSql = `SELECT count(*) AS totals from e_course WHERE title LIKE '%${keyword}%';`;
    let [, [{ totals }]] = await myQuery(tSql);
    let allPages = Math.ceil(totals / pageNum);
    let sql = `SELECT id,cid,title,price,CONCAT('${apiUrl}','/course/',image_src) AS image_src,intro,type_name,subject_name,area_name,grade_name,category_name  from e_course WHERE title LIKE '%${keyword}%' LIMIT ${startNum},${pageNum};`;
    let [err,result]=await myQuery(sql);
    if (err) {
      next({ code: 500, msg: err });
    } else {
      res.send({code:200,msg:'ok',page,pageNum,allPages,totals,result});
    }
  })
module.exports=router;
